package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.AddOrLessEnum;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.SyncStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.IFriendsService;
import com.ibeeking.found.cop.b.rest.service.IFriendsTagsGroupService;
import com.ibeeking.found.cop.b.rest.service.IFriendsTagsService;
import com.ibeeking.found.cop.b.rest.service.IMqService;
import com.ibeeking.found.cop.service.common.constants.FriendsConstant;
import com.ibeeking.found.cop.service.common.dos.FriendsTagsGroupDO;
import com.ibeeking.found.cop.service.common.dos.FriendsTagsDO;
import com.ibeeking.found.cop.service.common.dos.FriendsTagsRelDO;
import com.ibeeking.found.cop.service.common.enums.MqSyncOperatorTypeEnum;
import com.ibeeking.found.cop.service.common.param.FriendsTagsBatchParam;
import com.ibeeking.found.cop.service.common.param.FriendsTagsGroupParam;
import com.ibeeking.found.cop.service.common.param.FriendsTagsParam;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.found.cop.service.common.query.FriendsPageQuery;
import com.ibeeking.found.cop.service.common.query.FriendsTagsPageQuery;
import com.ibeeking.found.cop.service.common.query.FriendsTagsQuery;
import com.ibeeking.found.cop.service.common.vo.FriendsTagsVO;
import com.ibeeking.found.cop.service.common.vo.FriendsVO;
import com.ibeeking.found.cop.service.mapper.FriendsTagsMapper;
import com.ibeeking.found.cop.service.mapper.FriendsTagsRelMapper;
import com.ibeeking.found.cop.service.mapper.FriendsTagsGroupMapper;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.DataUtils;
import com.ibeeking.nematos.utils.date.DateUtil;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName FriendsTagsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class FriendsTagsServiceImpl extends ServiceImpl<FriendsTagsMapper, FriendsTagsDO> implements IFriendsTagsService {

    @Resource
    private FriendsTagsGroupMapper friendsTagsGroupMapper;

    @Resource
    private FriendsTagsMapper friendsTagsMapper;

    @Resource
    private FriendsTagsRelMapper friendsTagsRelMapper;

    @Resource
    private IFriendsService friendsSeviceImpl;

    @Resource
    private IFriendsTagsGroupService friendsTagsGroupSeviceImpl;

    @Resource
    private IMqService mqServiceImpl;

    @Override
    public List<FriendsTagsVO> list(FriendsTagsQuery query) {
        List<FriendsTagsDO> entityDOS = friendsTagsMapper.selectList(new LambdaQueryWrapper<FriendsTagsDO>()
                .eq(query.getId() != null, FriendsTagsDO::getId, query.getId())
                .like(StringUtils.isNotBlank(query.getTagsName()), FriendsTagsDO::getTagsName, query.getTagsName())
                .eq(query.getStatus() != null, FriendsTagsDO::getStatus, query.getStatus())
                .orderByDesc(FriendsTagsDO::getCreateTime)
        );
        List<FriendsTagsVO> entityVOS =JsonUtils.jsonToList(entityDOS, FriendsTagsVO.class);
        entityVOS.forEach(x -> {
            x.setStatusDesc(PublishStatusEnum.getName(x.getStatus()));
            x.setSyncStatusDesc(SyncStatusEnum.getName(x.getSyncStatus()));
        });
        return entityVOS;
    }

    @Override
    public Page<FriendsTagsVO> pageList(FriendsTagsPageQuery pageQuery) {
        LambdaQueryWrapper<FriendsTagsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, FriendsTagsDO::getId, pageQuery.getId())
                .like(StringUtils.isNotBlank(pageQuery.getTagsName()), FriendsTagsDO::getTagsName, pageQuery.getTagsName())
                .eq(pageQuery.getStatus() != null, FriendsTagsDO::getStatus, pageQuery.getStatus())
                .orderByDesc(FriendsTagsDO::getCreateTime);
        Page<FriendsTagsDO> entityDOPages = friendsTagsMapper.selectPage(new Page<FriendsTagsDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<FriendsTagsVO> entityVOPages = BeanUtil.convertPage(entityDOPages, FriendsTagsVO.class);
        entityVOPages.getRecords().forEach(f ->{
            f.setStatusDesc(PublishStatusEnum.getName(f.getStatus()));
            f.setSyncStatusDesc(SyncStatusEnum.getName(f.getSyncStatus()));
        });
        return entityVOPages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeTagsBatch(FriendsTagsBatchParam param, CurrentUser currentUser) {
        Integer addOrLess = param.getAddOrLess();
        if (AddOrLessEnum.ADD.getValue().equals(addOrLess)) {
            LogUtils.info("=====add FriendsTagsBatchParam:{}=========",JsonUtils.toJsonStr(param));
            this.addTagsBatch(param, currentUser);
        } else if (AddOrLessEnum.LESS.getValue().equals(addOrLess)) {
            LogUtils.info("=====del FriendsTagsBatchParam:{}=========",JsonUtils.toJsonStr(param));
            this.delTagsBatch(param, currentUser);
        } else {
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addTagsBatch(FriendsTagsBatchParam param, CurrentUser currentUser) {
        List<FriendsTagsRelDO> friendsTagsRelRsultDOS= new ArrayList<>();
        Integer type = param.getType();
        Set<Long> tagsIds = param.getTagsIds();
        List<Long> newFriendsIds = param.getFriendsIds();
        FriendsPageQuery condition = param.getCondition();
        if(type!=null && type==2){
            if(condition!=null){
                Page<FriendsVO> friendsVOPage = friendsSeviceImpl.pageList(condition);
                if(friendsVOPage!=null && friendsVOPage.getSize() >0){
                    newFriendsIds = friendsVOPage.getRecords().stream().map(FriendsVO::getId).collect(Collectors.toList());
                }else{
                    newFriendsIds = Collections3.emptyList();
                }
            }
        }
        LogUtils.info("==== add newFriendsIds:{}========",JsonUtils.toJsonStr(newFriendsIds));
        if(newFriendsIds == null || newFriendsIds.size() <=0) {
          throw new BusinessException("请至少选择一个客户！");
        }
        if(tagsIds ==null && tagsIds.size() <= 0){
            throw new BusinessException("请至少选择一个标签！");
        }

        for(Long tagsId: tagsIds){
           List<FriendsTagsRelDO> friendsTagsRelDOS = friendsTagsRelMapper.selectList(new LambdaQueryWrapper<FriendsTagsRelDO>().eq(FriendsTagsRelDO::getTagsId, tagsId));
           if (Collections3.isNotEmpty(friendsTagsRelDOS)) {
               List<Long> oldFriendsIds = friendsTagsRelDOS.stream().map(FriendsTagsRelDO::getFriendsId).collect(Collectors.toList());
               HashSet<Long> olsHashFriendsIds = new HashSet<>(oldFriendsIds);
               HashSet<Long> newHashFriendsIds = new HashSet<>(newFriendsIds);
               newHashFriendsIds.removeAll(olsHashFriendsIds);
               if (newHashFriendsIds != null && newHashFriendsIds.size() > 0) {
                   newHashFriendsIds.stream().forEach(f -> {
                       friendsTagsRelRsultDOS.add(new FriendsTagsRelDO(GenerateId.generateId(), f.longValue(), tagsId));
                   });
               }
           }else{
               if (newFriendsIds != null && newFriendsIds.size() > 0) {
                   newFriendsIds.stream().forEach(f -> {
                       friendsTagsRelRsultDOS.add(new FriendsTagsRelDO(GenerateId.generateId(), f.longValue(), tagsId));
                   });
               }
           }
        };
        LogUtils.info("====friendsTagsRelRsultDOS:{}========",JsonUtils.toJsonStr(friendsTagsRelRsultDOS));
        if(friendsTagsRelRsultDOS !=null && friendsTagsRelRsultDOS.size() >0){
            return friendsTagsRelMapper.insertBatchSomeColumn(friendsTagsRelRsultDOS) >0;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delTagsBatch(FriendsTagsBatchParam param, CurrentUser currentUser) {
        List<Long> friendsTagsRelIdS= new ArrayList<>();

        Integer type = param.getType();
        Set<Long> tagsIds = param.getTagsIds();
        List<Long> newFriendsIds = param.getFriendsIds();
        FriendsPageQuery condition = param.getCondition();
        if(condition == null || condition.getTagsIds() == null || condition.getTagsIds().size() <=0) throw new BusinessException("必须基于标签条件筛选后才能统一删除标签！");
        LogUtils.info("====condition:{}========",JsonUtils.toJsonStr(condition));
        if(type!=null && type==2){
            Page<FriendsVO> friendsVOPage = friendsSeviceImpl.pageList(condition);
            if(friendsVOPage!=null && friendsVOPage.getSize() >0){
                newFriendsIds = friendsVOPage.getRecords().stream().map(FriendsVO::getId).collect(Collectors.toList());
            }else{
                newFriendsIds = Collections3.emptyList();
            }
        }
        if(newFriendsIds == null || newFriendsIds.size() <=0) {
            throw new BusinessException("请至少选择一个客户！");
        }
        if(tagsIds ==null && tagsIds.size() <= 0){
            throw new BusinessException("请至少选择一个标签！");
        }
        LogUtils.info("====newFriendsIds:{}========",JsonUtils.toJsonStr(newFriendsIds));
        for(Long tagsId: tagsIds){
            List<FriendsTagsRelDO> friendsTagsRelDOS = friendsTagsRelMapper.selectList(new LambdaQueryWrapper<FriendsTagsRelDO>()
                    .eq(FriendsTagsRelDO::getTagsId, tagsId).in(FriendsTagsRelDO::getFriendsId,newFriendsIds));
            if (Collections3.isNotEmpty(friendsTagsRelDOS)) {
                List<Long> oldRelIds = friendsTagsRelDOS.stream().map(FriendsTagsRelDO::getId).collect(Collectors.toList());
                friendsTagsRelIdS.addAll(oldRelIds);
            }
        };
        LogUtils.info("====friendsTagsRelIdS:{}========",JsonUtils.toJsonStr(friendsTagsRelIdS));
        if(friendsTagsRelIdS !=null && friendsTagsRelIdS.size() >0){
            return friendsTagsRelMapper.deleteBatchIds(friendsTagsRelIdS) >0;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(FriendsTagsParam param) {
        checkUnique(param);
        FriendsTagsDO entityDO = BeanUtil.convertBean(param, FriendsTagsDO.class);
        entityDO.setId(GenerateId.generateId());
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsTagsMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addBatch(List<FriendsTagsRelDO> addList) {
        if (Collections3.isEmpty(addList)) {
            return false;
        }
        List<Long> idList = addList.stream().map(FriendsTagsRelDO::getId).collect(Collectors.toList());
        List<FriendsTagsRelDO> oldList = friendsTagsRelMapper.selectBatchIds(idList);
        if (Collections3.isNotEmpty(oldList)) {
            List<Long> oldIdList = oldList.stream().map(FriendsTagsRelDO::getId).collect(Collectors.toList());
            addList.removeIf(x -> oldIdList.contains(x.getId()));
        }
        if (Collections3.isEmpty(addList)) {
            return true;
        }
        DataUtils.groupList(addList, FriendsConstant.BATCH_SIZE).forEach(inList -> {
            friendsTagsRelMapper.insertBatchSomeColumn(inList);
        });
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(FriendsTagsParam param) {
        FriendsTagsDO entityDO = BeanUtil.convertBean(param, FriendsTagsDO.class);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsTagsMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateBatch(List<FriendsTagsDO> entityDOS) {
        return super.updateBatchById(entityDOS);
    }

    @Override
    public FriendsTagsVO view(Long id) {
        FriendsTagsDO entityDO = friendsTagsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        FriendsTagsVO entityVO = BeanUtil.convertBean(entityDO, FriendsTagsVO.class);
        if(entityVO != null) {
            entityVO.setStatusDesc(PublishStatusEnum.getName(entityDO.getStatus()));
            entityVO.setSyncStatusDesc(SyncStatusEnum.getName(entityVO.getSyncStatus()));
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        FriendsTagsDO entityDO = friendsTagsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        entityDO.setStatus(status);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsTagsMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        FriendsTagsDO entityDO = friendsTagsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return friendsTagsMapper.deleteById(id) > 0;
    }
    /**
     * 从企业微信同步客户信息
     *
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer syncFriendsTagsFromWx(){
        String tenantId = UserUtils.getTenantId();
        MqSyncCopParam mqParam = new MqSyncCopParam();
        mqParam.setTenantId(Long.valueOf(tenantId));
        mqParam.setOperType(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_TAGS_LIST.getCode());
        mqServiceImpl.sendSyncToWxTaskMessage(mqParam);
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveOrUpdateOrDeleteBatchFriendsTags(Long tenantId, List<FriendsTagsGroupParam> params){
        if(Collections3.isNotEmpty(params)) {
            List<Long> friendsTagsTypeIds = new ArrayList<>();
            List<FriendsTagsGroupDO> needInsertFriendsTagsGroupDOS = new ArrayList<>();
            List<FriendsTagsGroupDO> needUpdateFriendsTagsGroupDOS = new ArrayList<>();
            List<FriendsTagsGroupDO> needDeleteFriendsTagsGroupDOS = new ArrayList<>();

            List<FriendsTagsDO> needInsertFriendsTagsDOS = new ArrayList<>();
            List<FriendsTagsDO> needUpdateFriendsTagsDOS = new ArrayList<>();
            List<FriendsTagsDO> needDeleteFriendsTagsDOS = new ArrayList<>();
            List<Long> friendsTagsIds = new ArrayList<>();

            Map<String, Long> friendsTagsTypeCodeAndIdMap = null;

            List<FriendsTagsGroupDO> oldFriendsTagsGroupDO = friendsTagsGroupMapper.selectList(new LambdaQueryWrapper<FriendsTagsGroupDO>());
            if(Collections3.isNotEmpty(oldFriendsTagsGroupDO)){
                friendsTagsTypeCodeAndIdMap = oldFriendsTagsGroupDO.parallelStream().collect(Collectors.toMap(FriendsTagsGroupDO::getGroupCode, FriendsTagsGroupDO::getId, (o, n) -> o));
                List<String> oldFriendsTagsTypeCodeList = oldFriendsTagsGroupDO.parallelStream().map(FriendsTagsGroupDO::getGroupCode).collect(Collectors.toList());
                List<String> newFriendsTagsTypeCodeList = params.parallelStream().map(FriendsTagsGroupParam::getGroupCode).collect(Collectors.toList());
                if (Collections3.isNotEmpty(oldFriendsTagsTypeCodeList) && Collections3.isNotEmpty(newFriendsTagsTypeCodeList)) {
                    HashSet<String> oldFriendsTagsTypeCodeHash = new HashSet<>(oldFriendsTagsTypeCodeList);
                    LogUtils.info("====oldGroupChatIdHash:{}====", JsonUtils.toJsonStr(oldFriendsTagsTypeCodeHash));
                    HashSet<String> newFriendsTagsTypeCodeHash = new HashSet<>(newFriendsTagsTypeCodeList);
                    LogUtils.info("====newGroupChatIdHash:{}====", JsonUtils.toJsonStr(newFriendsTagsTypeCodeHash));
                    oldFriendsTagsTypeCodeHash.removeAll(newFriendsTagsTypeCodeHash);
                    LogUtils.info("==== remove new oldGroupChatIdHash:{}====", JsonUtils.toJsonStr(oldFriendsTagsTypeCodeHash));
                    if (oldFriendsTagsTypeCodeHash.size() > 0) {
                        List<String> needFriendsTagsTypeCodeIds = new ArrayList<>(oldFriendsTagsTypeCodeHash);
                        LogUtils.info("==== needFriendsTagsTypeCodeIds:{}====", JsonUtils.toJsonStr(needFriendsTagsTypeCodeIds));
                        needDeleteFriendsTagsGroupDOS = friendsTagsGroupMapper.selectList(new LambdaQueryWrapper<FriendsTagsGroupDO>().in(FriendsTagsGroupDO::getGroupCode, needFriendsTagsTypeCodeIds));
                    }
                    if(Collections3.isNotEmpty(needDeleteFriendsTagsGroupDOS)){
                        List<Long> askFriendTypeIds = needDeleteFriendsTagsGroupDOS.parallelStream().map(FriendsTagsGroupDO::getId).collect(Collectors.toList());
                        List<FriendsTagsDO> askDeleteFriendsTagsDOS = friendsTagsMapper.selectList(new LambdaQueryWrapper<FriendsTagsDO>().in(FriendsTagsDO::getGroupId, askFriendTypeIds));
                        needDeleteFriendsTagsDOS.addAll(askDeleteFriendsTagsDOS);
                    }
                }
            }
            LogUtils.info("====needDeleteFriendsTagsDOS:{}====", JsonUtils.toJsonStr(needDeleteFriendsTagsDOS));
            for(FriendsTagsGroupParam param: params){
                FriendsTagsGroupDO friendsTagsGroupDO = BeanUtil.convertBean(param, FriendsTagsGroupDO.class);
                Long tagTypeId = 0L;
                friendsTagsGroupDO.setCreateTime(LocalDateTime.now());
                friendsTagsGroupDO.setModifyTime(LocalDateTime.now());
                if(StringUtils.isNotEmpty(param.getBuildTime())){
                    LocalDateTime buildTime = DateUtil.strToLocalDateTime(param.getBuildTime());
                    friendsTagsGroupDO.setBuildTime(buildTime);
                }
                if(friendsTagsTypeCodeAndIdMap !=null && friendsTagsTypeCodeAndIdMap.containsKey(param.getGroupCode())){
                    tagTypeId = friendsTagsTypeCodeAndIdMap.get(param.getGroupCode());
                    friendsTagsGroupDO.setId(tagTypeId);
                    needUpdateFriendsTagsGroupDOS.add(friendsTagsGroupDO);
                    friendsTagsTypeIds.add(tagTypeId);

                    List<FriendsTagsParam> tagsParamList = param.getTagsList();
                    if(Collections3.isNotEmpty(tagsParamList)) {
                        Map<String, Long> friendsTagsCodeAndIdMap = null;
                        List<FriendsTagsDO> oldFriendsTagsDOS = friendsTagsMapper.selectList(new LambdaQueryWrapper<FriendsTagsDO>().eq(FriendsTagsDO::getGroupId, tagTypeId));
                        if(Collections3.isNotEmpty(oldFriendsTagsDOS)){
                            friendsTagsCodeAndIdMap = oldFriendsTagsDOS.parallelStream().collect(Collectors.toMap(FriendsTagsDO::getTagsCode, FriendsTagsDO::getId, (o, n) -> o));
                            List<String> oldFriendsTagsCodeList = oldFriendsTagsDOS.parallelStream().map(FriendsTagsDO::getTagsCode).collect(Collectors.toList());
                            List<String> newFriendsTagsCodeList = tagsParamList.parallelStream().map(FriendsTagsParam::getTagsCode).collect(Collectors.toList());
                            if (Collections3.isNotEmpty(oldFriendsTagsCodeList) && Collections3.isNotEmpty(newFriendsTagsCodeList)) {
                                HashSet<String> oldFriendsTagsCodeHash = new HashSet<>(oldFriendsTagsCodeList);
                                HashSet<String> newFriendsTagsCodeHash = new HashSet<>(newFriendsTagsCodeList);
                                oldFriendsTagsCodeHash.removeAll(newFriendsTagsCodeHash);
                                if (oldFriendsTagsCodeHash!=null && oldFriendsTagsCodeHash.size() > 0) {
                                    List<String> needFriendsTagsCodeIds = new ArrayList<>(oldFriendsTagsCodeHash);
                                    List<FriendsTagsDO> askDeleteFriendsTagsDOS = friendsTagsMapper.selectList(new LambdaQueryWrapper<FriendsTagsDO>().in(FriendsTagsDO::getTagsCode, needFriendsTagsCodeIds));
                                    needDeleteFriendsTagsDOS.addAll(askDeleteFriendsTagsDOS);
                                }
                            }
                        }
                        for (FriendsTagsParam d : tagsParamList) {
                            FriendsTagsDO friendsTagsDO = BeanUtil.convertBean(d, FriendsTagsDO.class);
                            Long tagsId = 0L;
                            friendsTagsDO.setCreateTime(LocalDateTime.now());
                            friendsTagsDO.setModifyTime(LocalDateTime.now());
                            friendsTagsDO.setStatus(1);
                            if(StringUtils.isNotEmpty(d.getBuildTime())){
                                LocalDateTime buildTime = DateUtil.strToLocalDateTime(d.getBuildTime());
                                friendsTagsDO.setBuildTime(buildTime);
                            }
                            if(friendsTagsCodeAndIdMap !=null && friendsTagsCodeAndIdMap.containsKey(d.getTagsCode())){
                                tagsId = friendsTagsCodeAndIdMap.get(d.getTagsCode());
                                friendsTagsDO.setId(tagsId);
                                needUpdateFriendsTagsDOS.add(friendsTagsDO);
                                friendsTagsIds.add(tagsId);
                            }else{
                                tagsId = IdGenerator.getId();
                                friendsTagsDO.setId(tagsId);
                                needInsertFriendsTagsDOS.add(friendsTagsDO);
                                friendsTagsIds.add(tagsId);
                            }
                        }
                    }
                }else{
                    tagTypeId = IdGenerator.getId();
                    friendsTagsGroupDO.setId(tagTypeId);
                    needInsertFriendsTagsGroupDOS.add(friendsTagsGroupDO);
                    friendsTagsTypeIds.add(tagTypeId);
                    List<FriendsTagsParam> tagsParamList = param.getTagsList();
                    if(Collections3.isNotEmpty(tagsParamList)){
                        for(FriendsTagsParam d:tagsParamList){
                            FriendsTagsDO friendsTagsDO = BeanUtil.convertBean(d, FriendsTagsDO.class);
                            friendsTagsDO.setCreateTime(LocalDateTime.now());
                            friendsTagsDO.setModifyTime(LocalDateTime.now());
                            friendsTagsDO.setStatus(1);
                            friendsTagsDO.setId(IdGenerator.getId());
                            friendsTagsDO.setGroupId(tagTypeId);
                            if(StringUtils.isNotEmpty(d.getBuildTime())){
                                LocalDateTime buildTime = DateUtil.strToLocalDateTime(d.getBuildTime());
                                friendsTagsDO.setBuildTime(buildTime);
                            }
                            needInsertFriendsTagsDOS.add(friendsTagsDO);
                        }
                    }
                }
            }
            if(Collections3.isNotEmpty(needInsertFriendsTagsGroupDOS)){
                LogUtils.info("====needInsertFriendsTagsGroupDOS:{}====", JsonUtils.toJsonStr(needInsertFriendsTagsGroupDOS));
                friendsTagsGroupMapper.insertBatchSomeColumn(needInsertFriendsTagsGroupDOS);
            }
            if(Collections3.isNotEmpty(needUpdateFriendsTagsGroupDOS)){
                LogUtils.info("====needUpdateFriendsTagsGroupDOS:{}====", JsonUtils.toJsonStr(needUpdateFriendsTagsGroupDOS));
                friendsTagsGroupSeviceImpl.updateBatchById(needUpdateFriendsTagsGroupDOS);
            }
            if(Collections3.isNotEmpty(needDeleteFriendsTagsGroupDOS)){
                LogUtils.info("====needDeleteFriendsTagsGroupDOS:{}====", JsonUtils.toJsonStr(needDeleteFriendsTagsGroupDOS));
                List<Long> typeIds = needDeleteFriendsTagsGroupDOS.parallelStream().map(FriendsTagsGroupDO::getId).collect(Collectors.toList());
                friendsTagsGroupMapper.deleteBatchIds(typeIds);
            }
            if(Collections3.isNotEmpty(needInsertFriendsTagsDOS)){
                LogUtils.info("====needInsertFriendsTagsDOS:{}====", JsonUtils.toJsonStr(needInsertFriendsTagsDOS));
                friendsTagsMapper.insertBatchSomeColumn(needInsertFriendsTagsDOS);
            }
            if(Collections3.isNotEmpty(needUpdateFriendsTagsDOS)){
                LogUtils.info("====needUpdateFriendsTagsDOS:{}====", JsonUtils.toJsonStr(needUpdateFriendsTagsDOS));
                super.updateBatchById(needUpdateFriendsTagsDOS);
            }
            if(Collections3.isNotEmpty(needDeleteFriendsTagsDOS)){
                LogUtils.info("====needDeleteFriendsTagsDOS:{}====", JsonUtils.toJsonStr(needDeleteFriendsTagsDOS));
                List<Long> tagsIds = needDeleteFriendsTagsDOS.parallelStream().map(FriendsTagsDO::getId).collect(Collectors.toList());
                friendsTagsMapper.deleteBatchIds(tagsIds);
            }

        }
        return 1;
    }
    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(FriendsTagsParam param) {
        if (param == null) {
            throw new BusinessException("当前记录不存在!");
        }
        //code name确保等级唯一性
        LambdaQueryWrapper<FriendsTagsDO> queryWrapper = new LambdaQueryWrapper<FriendsTagsDO>()
                .ne(param.getId() != null, FriendsTagsDO::getId, param.getId())
                .eq(FriendsTagsDO::getTagsName, param.getTagsName());
        List<FriendsTagsDO> dos = friendsTagsMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在同名的记录!");
        }
    }

}
